<?
	include_once "../../include/conn.php";
	include_once "../../api/config.php";
	/**************************************
	 * ************ 获取区域列表 ***********
	 * ************************************/
	$limit = isset($_GET['limit'])? $_GET['limit'] :20;//默认每页数量 ，和前台layui对应
	$page = isset($_GET['page'])? $_GET['page'] :1; //分页参数

	

	// 用于输出的json数据
	$results = array('code' => 0,'msg'=>'数据正常获取！');//结果

	//查询语句前缀
	$cmdPrex = "
		select scope.*,area.name area_name ,city.name city_name 
		from vbyzc_vote_scope scope 
		left join(select * from area) area on area.areaid = scope.areaid
		left join(select * from city) city on city.cityid = area.cityid
	";

	// 条件处理
	if($areaid = @$_GET['areaid']){
		$cmdPrex .= "where scope.areaid = '$areaid' ";
	}

	//先得到记录总数，没有带limit分页的。加到结果数据中
	$record_count = $mydb->num_rows($cmdPrex);
	$results['count'] = $record_count;

	// 加入排序
	$cmd_result = $cmdPrex." order by id ";
	
	//处理limit翻页,查询结果返回前端 
	$page_left  = $page*$limit - $limit;
	$cmd_result .= " limit $page_left,$limit"; //加入分页
	$rs = mysql_query($cmd_result); //执行

	// 取出数据
	while ($row = mysql_fetch_assoc($rs)) {
		$results['data'][] = Array(
			'id' => $row['id'],
			'name' => $row['name'],
			'city_name' => $row['city_name'],
			'area_name' => $row['area_name'],
			'areaid' => $row['areaid']
		) ;
	};

	// //////////////////输出数据////////////////////////////////
	echo decodeUnicode(json_encode($results));
	// print_r($results);
	mysql_close();
?>